from django.db import models
from administrators.models import Userinfo


# 处理方分组
class HandleGroup(models.Model):
    name = models.CharField(max_length=20, unique=True)
    mnemonic = models.CharField(max_length=20, unique=True)
    operate_date = models.DateField()
    operator = models.ForeignKey(Userinfo, on_delete=models.PROTECT)
    enable = models.BooleanField()


# 处理方
class Handle(models.Model):
    name = models.CharField(max_length=30, unique=True)
    mnemonic = models.CharField(max_length=30, unique=True)
    operate_date = models.DateField()
    operator = models.ForeignKey(Userinfo, on_delete=models.PROTECT)
    enable = models.BooleanField()
    handle_group = models.ForeignKey(HandleGroup, on_delete=models.PROTECT)


# 物料分组表
class MaterialGroup(models.Model):
    mnemonic = models.CharField(max_length=100, unique=True)
    name = models.CharField(max_length=100, unique=True)
    operator = models.ForeignKey(Userinfo, on_delete=models.PROTECT)
    enable = models.BooleanField()  # 是否启用
    operate_date = models.DateField()
    weight_fix = models.DecimalField(max_digits=11, decimal_places=3)  # 正品实际重量修正


# 钢铁材质表
# 可被允许的内容：195 215 235 345 355
class SteelMaterial(models.Model):
    name = models.CharField(max_length=100, unique=True)


# 物料表
class Material(models.Model):
    code = models.CharField(max_length=100, unique=True)
    name = models.CharField(max_length=100)
    model = models.CharField(max_length=100)
    group = models.ForeignKey(MaterialGroup, on_delete=models.PROTECT)
    steel_material = models.ForeignKey(SteelMaterial, on_delete=models.PROTECT)
    set_piece = models.IntegerField()  # 一件内含支数
    operator = models.ForeignKey(Userinfo, on_delete=models.PROTECT)
    enable = models.BooleanField()  # 是否启用
    operate_date = models.DateField()
    theory_scatt_weight = models.DecimalField(max_digits=11, decimal_places=3)  # 每支的理论重量
    # 正品库
    whole_piece = models.IntegerField()  # 件数
    scatt_piece = models.IntegerField()  # 散支
    weight = models.DecimalField(max_digits=11, decimal_places=3)  # 实际总重量（通过加减得来）
    theory_weight = models.DecimalField(max_digits=11, decimal_places=3)  # 理论总重量
    latest_date = models.DateField()  # 最新操作发生的日期


# 物料增减表
class Record(models.Model):
    material = models.ForeignKey(Material, on_delete=models.PROTECT)
    trend = models.BooleanField()  # false为如，true为出
    whole_piece = models.IntegerField()  # 件数
    scatt_piece = models.IntegerField()  # 散支
    weight = models.DecimalField(max_digits=11, decimal_places=3)  # 吨数
    remark = models.CharField(max_length=100)
    handle = models.ForeignKey(Handle, on_delete=models.PROTECT)
    actual_date = models.DateField()  # 物料出入库的实际日期
    operate_date = models.DateField()  # 在系统上进行该操作的日期
    operator = models.ForeignKey(Userinfo, on_delete=models.PROTECT)


# 每日详情汇总表
class Summary(models.Model):
    actual_date = models.DateField()
    material_group = models.ForeignKey(MaterialGroup, on_delete=models.PROTECT)  # 物料分组
    inbound = models.DecimalField(max_digits=11, decimal_places=3)  # 入库吨数
    outbound = models.DecimalField(max_digits=11, decimal_places=3)  # 出库吨数
    other_in = models.DecimalField(max_digits=11, decimal_places=3)  # 其他渠道入
    other_out = models.DecimalField(max_digits=11, decimal_places=3)  # 其他渠道出
    total_out = models.DecimalField(max_digits=11, decimal_places=3)  # 当天出总计
    remark = models.CharField(max_length=100)  # 备注
    operate_date = models.DateField()
    operator = models.ForeignKey(Userinfo, on_delete=models.PROTECT)
